package jangdolbaeng2.seller.dao;

import jangdolbaeng2.db.MybatisConfig;
import jangdolbaeng2.vo.ShopVO;
import jangdolbaeng2.vo.Shop_blogVO;

import java.util.HashMap;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

/*
 * 작성자 : 진승민
 * 기능 : DB의 Blog Table 제어해 블로그 제작에 필요한 자료를 주고받는다.*/

public class SellerDao {
	SqlSessionFactory factory = MybatisConfig.getSqlSessionFactory();

	// 블로그 설정정보를 읽어들임
	public Shop_blogVO blogSetup(int shop_seq) {
		SqlSession ss = factory.openSession();
		Shop_blogVO blogSetting = null;
		try {
			blogSetting = ss.selectOne("seller.blogSetup", shop_seq);
			ss.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			if (ss != null) ss.close();
		}
		//System.out.println("blogsetup"+blogSetting);
		return blogSetting;
	}

	// 카테고리 정보를 리스트로 읽어서 HashMap으로 변환
	@SuppressWarnings("unchecked")
	public HashMap<String, String> category() {
		SqlSession ss = factory.openSession();
		HashMap<String, String> map = new HashMap<String, String>();
		List list = null;
		try {
			list = ss.selectList("seller.category");
			ss.commit();
			for (Object x : list) {
				map.put(((HashMap<String, String>) x).get("CATEGORY_KEY"),
						((HashMap<String, String>) x).get("CATEGORY_VALUE"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			if (ss != null) ss.close();
		}
		return map;
	}


	// 블로그 설정정보 리스트를 생성
	public List<HashMap<String,Object>> blogList(String category) {
		List<HashMap<String,Object>> blogLists = null;
		SqlSession ss = factory.openSession();
		try {
			blogLists = ss.selectList("seller.blogList", category);
			ss.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			if (ss != null) ss.close();
		}
		return blogLists;
	}

	// 상점 리스트를 생성
	public List<ShopVO> shopList(String category) {
		List<ShopVO> list = null;
		SqlSession ss = factory.openSession();
		try {
			list = ss.selectList("seller.shopList", category);
			ss.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			if (ss != null) ss.close();
		}
		return list;
	}

	// 해당 상점 정보를 받아옴
	public ShopVO getShop(int shop_seq) {
		ShopVO shop = null;
		SqlSession ss = factory.openSession();
		try {
			shop = ss.selectOne("seller.getShop", shop_seq);
			ss.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			if (ss != null) ss.close();
		}

		return shop;
	}
}
